<?php
namespace Home\Controller;
class RepaycreditController extends HomeBaseController
{
    /**
     * 获得流水对应的还款记录
     *
     */
    public function repaymentlist(){
        $id=I('id','');
        if($id==''){
            $this->assign('error','参数错误!');
            $this->display('credit/repaymentlist');
            exit();
        }
        $credflow=M('CredFlow')->find($id);
        if(empty($credflow)){
            $this->assign('error','对应的借款流水不存在!');
            $this->display('credit/repaymentlist');
            exit();
        }
        $repayFlowlist=M('RepaymentFlow')->where('cred_flowid='.$id)->order('stage')->select();
        if(empty($repayFlowlist)){
            $this->assign('error','查询到的还款记录为空!');
            $this->display('credit/repaymentlist');
            exit();
        }else{
            $user=M('User')->field('username')->find($credflow['loanduid']);
            $this->assign('list',$repayFlowlist);
            $this->assign("credflow",$credflow);
            $this->assign('username',$user['username']);
            $ty=I('ty','');
            $this->assign("ty",$ty);
            $this->display('credit/repaymentlist');
            exit();
        }
    }
    
    public function torepayment(){
        $id=I('id','');
        $authpaswd=I('authpaswd','');
        if($id=='' || $authpaswd==''){
            $this->ajaxReturn(array('code'=>'error','info' => '参数错误!'));//0错误，1成功
            exit();
        }
        $transpasswd=M('User')->field('transpasswd')->find(session('uid'));
        if($transpasswd['transpasswd']!=md5($authpaswd)){
            $this->ajaxReturn(array('code'=>'error','info' => '支付密码错误,请重新输入!'));//0错误，1成功
            exit();
        }
        $RepaymentFlow=M('RepaymentFlow')->find($id);
        if(empty($RepaymentFlow)){
            $this->ajaxReturn(array('code'=>'error','info' => '还款流水不存在!'));//0错误，1成功
            exit();
        }
        $credflow=M('CredFlow')->find($RepaymentFlow['cred_flowid']);
        if(empty($RepaymentFlow)){
            $this->ajaxReturn(array('code'=>'error','info' => '还款流水对应的借款流水不存在!'));//0错误，1成功
            exit();
        }
        
        /**
         * 判断是不是当前借款流水的最后一期还款
         */
        $leave=M('RepaymentFlow')->where('cred_flowid='.$credflow['id'].' and is_yq=3')->count();//获得已还款期数记录
        $shouyi=$credflow['interest']+$credflow['penalty'];//投资人收益
        $zhichu=$shouyi;//借款人支出
        $loanUseAsset= M('UserAssets')->field('avail_money,notpresent_money,present_money')->where('uid='.$credflow['loanduid'])->select();
        if(($leave+1)==$credflow['persionnum']){//为借款流水还款当前最后一期，但并不代表就一定是借款项目最后一期，因为一个借款项目可以同时产生多个流水(对应多个投资人)
            $zhichu=$zhichu+$credflow['mangrate']+$credflow['money'] ;
           $shouyi=$shouyi+$credflow['money'] ;
           if($loanUseAsset[0]['avail_money']<$zhichu){
               $this->ajaxReturn(array('code'=>'error','info' => '您的账户资金不足还款，请先充值!'));//0错误，1成功
               exit();
           }
           M('RepaymentFlow')->where('id='.$id)->setField(array('is_yq'=>3));
           M('CredFlow')->where('id='.$RepaymentFlow['cred_flowid'])->setField(array('status'=>2,'penalty'=>0));
           M('CredFlow')->where('id='.$RepaymentFlow['cred_flowid'])->setInc('rpersionum',1);//
           /**
            * 判断借款是不是从借款标里产生的，若是，则需要判断此借款标下所有借款流水是否都已还清
            */
           $shenyuCount=M('RepaymentFlow')->where("cred_code='".$credflow['cred_code']."' and is_yq in(1,2)")->count();
           if(empty($shenyuCount) || $shenyuCount==0){//已经还清所有借款
                   $domainlist= M('CredflowDomian')->field('domainid')->where("cred_code='".$credflow['cred_code']."'")->select();
                   foreach ($domainlist as $dom){
                       M('UserDomains')->where('id='.$dom['domainid'])->setField(array('is_mortage'=>1));
                   }
                   if(!empty($credflow['loanid'])){
                       M('LoaderInfo')->where('id='.$credflow['loanid'])->setField(array('status'=>4));//结束
                   }
           }
           
           /**
            * 平台收益表添加一笔手续费收入记录
            */
           
          $platform_mort= M('PlatformProfitlog');
          $platfDat['money']=$credflow['mangrate'];
          $platfDat['comefr']=4;
          $platfDat['userid']=session('uid');
          $platfDat['descr']='用户还款扣除管理费';
          $platfDat['createdate']=date('y-m-d H:m:s',time());
          $platform_mort->add($platfDat);
        }else{
            if($loanUseAsset[0]['avail_money']<$zhichu){
                $this->ajaxReturn(array('code'=>'error','info' => '您的不可提现账户资金不足，请先充值!'));//0错误，1成功
                exit();
            }
            M('CredFlow')->where('id='.$RepaymentFlow['cred_flowid'])->setField(array('penalty'=>0));
            M('CredFlow')->where('id='.$RepaymentFlow['cred_flowid'])->setInc('rpersionum',1);//
            M('RepaymentFlow')->where('id='.$id)->setField(array('is_yq'=>3));
        }
       
        /**
         * 投资人资金
         */
        M('UserAssets')->where('uid='.$credflow['investuid'])->setInc('avail_money',$shouyi);
        M('UserAssets')->where('uid='.$credflow['investuid'])->setInc('present_money',$shouyi);
        /**
         * 投资人收入明细标
         */
        $userCaptdetail=M('UserCaptdetail');
        $userCapt['uid']=$credflow['investuid'];
        $userCapt['tran_income']=5;
        $userCapt['flow_money']=$shouyi;
        $userCapt['tran_type']=1;
        $userCapt['createdate']=date('y-m-d H:m:s',time());
        $userCaptdetail->add($userCapt);
       
        /**
         * 借款人支出
         */
        if($loanUseAsset[0]['notpresent_money']<$zhichu){
            M('UserAssets')->where('uid='.$credflow['loanduid'])->setDec('avail_money',$zhichu);
            M('UserAssets')->where('uid='.$credflow['loanduid'])->setDec('notpresent_money',$loanUseAsset[0]['notpresent_money']);
            M('UserAssets')->where('uid='.$credflow['loanduid'])->setDec('present_money',($zhichu-$loanUseAsset[0]['notpresent_money']));
        }else{
            M('UserAssets')->where('uid='.$credflow['loanduid'])->setDec('avail_money',$zhichu);
            M('UserAssets')->where('uid='.$credflow['loanduid'])->setDec('notpresent_money',$zhichu);
        }
        
        /**
         * 借款人支出明细
         */
        $userCapt['uid']=$credflow['loanduid'];
        $userCapt['tran_income']=5;
        $userCapt['flow_money']=$zhichu;
        $userCapt['tran_type']=2;
        $userCapt['createdate']=date('y-m-d H:m:s',time());
        $userCaptdetail->add($userCapt);
        $this->ajaxReturn(array('code'=>'success','info' => '还款成功!'));//0错误，1成功
        exit();
    }
   
}